package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.dto.RoleDto;
import com.bjsxt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.mapper.RoleMapper;
import com.bjsxt.domain.Role;
import com.bjsxt.service.RoleService;

/**
 * 系统角色接口实现类
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 分页查询角色
     *
     * @param roleDto
     * @return
     */
    @Override
    public DataGridView listRolePage(RoleDto roleDto) {
        Page<Role> page = new Page<>();
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(roleDto.getRoleName()), Role.COL_ROLE_NAME, roleDto.getRoleName());
        queryWrapper.like(StringUtils.isNotBlank(roleDto.getRoleCode()), Role.COL_ROLE_CODE, roleDto.getRoleCode());
        queryWrapper.eq(StringUtils.isNotBlank(roleDto.getStatus()), Role.COL_STATUS, roleDto.getStatus());
        queryWrapper.ge(null != roleDto.getBeginTime(), Role.COL_CREATE_TIME, roleDto.getBeginTime());
        queryWrapper.le(null != roleDto.getEndTime(), Role.COL_CREATE_TIME, roleDto.getEndTime());
        queryWrapper.orderByDesc(Role.COL_ROLE_SORT);
        this.roleMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 查询所有可用角色
     *
     * @return
     */
    @Override
    public List<Role> listAllRoles() {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Role.COL_STATUS, Constants.STATUS_TRUE);
        queryWrapper.orderByDesc(Role.COL_ROLE_SORT);
        return this.roleMapper.selectList(queryWrapper);
    }

    /**
     * 根据角色id查询角色
     *
     * @param roleId
     * @return
     */
    @Override
    public Role getOne(Long roleId) {
        return this.roleMapper.selectById(roleId);
    }

    /**
     * 添加角色
     *
     * @param roleDto
     * @return
     */
    @Override
    public int addRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDto, role);
//        设置创建人和创建时间
        role.setCreateBy(roleDto.getSimpleUser().getUserName());
        role.setCreateTime(DateUtil.date());
        return this.roleMapper.insert(role);
    }

    /**
     * 修改角色
     *
     * @param roleDto
     * @return
     */
    @Override
    public int updateRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDto, role);
//        设置修改人和修改时间
        role.setUpdateBy(roleDto.getSimpleUser().getUserName());
        role.setUpdateTime(DateUtil.date());
        return this.roleMapper.updateById(role);
    }

    /**
     * 根据角色id删除角色
     *
     * @param roleIds
     * @return
     */
    @Override
    public int deleteRoleByIds(Long[] roleIds) {
        List<Long> ids = Arrays.asList(roleIds);
        Role role = new Role();
        if (ids != null && ids.size() > 0) {
//            删除中间表sys_role_menu
            this.roleMapper.deleteRoleMenuByMenuIds(ids);
//            删除中间表sys_role_user
            this.roleMapper.deleteRoleUserByUserIds(ids);
            return this.roleMapper.deleteBatchIds(ids);
        }
        return 0;
    }

    /**
     * 保存角色和菜单之间的关系
     *
     * @param roleId
     * @param menuIds
     */
    @Override
    public void saveRoleMenu(Long roleId, Long[] menuIds) {
        this.roleMapper.deleteRoleMenuByMenuIds(Arrays.asList(roleId));
        for (Long menuId : menuIds) {
            this.roleMapper.saveRoleMenu(roleId, menuId);
        }
    }

    /**
     * 根据用户id查询用户拥有的角色ids
     *
     * @param userId
     * @return
     */
    @Override
    public List<Long> getRoleIdsByUserId(Long userId) {
        if (null == userId) {
//            Collections是一个包装类。它包含有各种有关集合操作的 静态多态方法，此类 不能实例化，就像一 个工具类，服务于Java的Collection框架
            return Collections.EMPTY_LIST;
        }
        return this.roleMapper.selectRoleIdsByUserId(userId);
    }

    /**
     * 保存角色和用户之间的关系
     *
     * @param userId
     * @param roleIds
     */
    @Override
    public void saveRoleUser(Long userId, Long[] roleIds) {
//        根据用户id先删除sys_role_menu里面的数据
        this.roleMapper.deleteRoleUserByUserIds(Arrays.asList(userId));
        for (Long roleId : roleIds) {
            this.roleMapper.saveRoleUser(userId, roleId);
        }
    }


}
